﻿using Model;
using Presenters.WPF;
using Presenters.WPF.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AirlinePilotAcademy_WPF.UI_Administrator.Subject
{
    /// <summary>
    /// Lógica de interacción para SubjectToTeacherSolution.xaml
    /// </summary>
    public partial class SubjectToTeacherSolution : Window, IAsignSubjectToTeacher
    {
        AsignSubjectToTeacherPresenter presenter;
        private string Message;
        private Horary isInHoraryObject;
        SubjectToTeacher subjectToTeacher;
        bool existHorary = true;
        private Model.Subject subjectSelected;

        public SubjectToTeacherSolution()
        {
            InitializeComponent();
            presenter = new AsignSubjectToTeacherPresenter(this);
            presenter.SearchSubjectInRealTime();
            presenter.SearchTeacherInRealTime();

            isInHoraryObject = new Horary();
            fillComboBox();
            //this.DataGrid_SubjectResult.SelectedIndex = 0;
            this.DataGrid_TeacherResult.SelectedIndex = 0;
            this.ComboBox_Gestion.SelectedIndex = DateTime.Now.Month - 1;
        }

        //Modify
        public SubjectToTeacherSolution(SubjectToTeacher subjectToTeacher)
        {
            InitializeComponent();
            this.Title = "Modificar materia docente";
            this.Button_Save.Visibility = Visibility.Hidden;
            this.Button_Modify.Visibility = Visibility.Visible;
            this.Button_Horary.Content = "Modificar horarios y aulas";
            this.subjectToTeacher = subjectToTeacher;

            presenter = new AsignSubjectToTeacherPresenter(this);
            presenter.SearchSubjectInRealTime();
            presenter.SearchTeacherInRealTime();

            isInHoraryObject = DataAccess.HoraryDataAccess.getHoraryActiveByCode(subjectToTeacher.code.ToString());
            if (isInHorary == null)
            {
                isInHorary = new Horary();
                existHorary = false;
            }
            fillComboBox();
            this.selectedSubject = DataAccess.SubjectDataAccess.getSubjectByCode(subjectToTeacher.codSubject.ToString(), true);

            //this.TextBox_FilterSubject.Text = DataAccess.SubjectDataAccess.getSubjectByCode(subjectToTeacher.codSubject.ToString(), true).nameSubject;
            this.TextBox_FilterTeacher.Text = DataAccess.TeacherDataAccess.getTeacherByCI(subjectToTeacher.ciTeacher).Name;
            this.Label_Teacher.Content = "Docente: " + DataAccess.TeacherDataAccess.getTeacherByCI(subjectToTeacher.ciTeacher).Name;
            this.Label_TeacherTeaching.Content = "Dicta la materia: " + this.selectedSubject.nameSubject;
            this.ComboBox_Gestion.SelectedItem = subjectToTeacher.gestion;

            //this.DataGrid_SubjectResult.SelectedIndex = 0;
            this.DataGrid_TeacherResult.SelectedIndex = 0;
            this.ComboBox_Gestion.SelectedItem = subjectToTeacher.gestion;
            this.ComboBox_Activity.SelectedValue = isInHorary.activity.ToString();
        }

        //Watch
        public SubjectToTeacherSolution(SubjectToTeacher subjectToTeacher, string buttonCloseText)
        {
            InitializeComponent();
            this.Title = "Ver materia docente";
            this.Button_Close.Content = buttonCloseText;
            this.Button_Save.Visibility = Visibility.Hidden;
            this.Button_Horary.Content = "Ver horarios y aulas";

            presenter = new AsignSubjectToTeacherPresenter(this);
            presenter.SearchSubjectInRealTime();
            presenter.SearchTeacherInRealTime();

            isInHoraryObject = DataAccess.HoraryDataAccess.getHoraryActiveByCode(subjectToTeacher.code.ToString());
            if (isInHorary == null)
            {
                isInHorary = new Horary();
            }
            fillComboBox();
            this.selectedSubject = DataAccess.SubjectDataAccess.getSubjectByCode(subjectToTeacher.codSubject.ToString(), true);

            //this.TextBox_FilterSubject.Text = DataAccess.SubjectDataAccess.getSubjectByCode(subjectToTeacher.codSubject.ToString(), true).nameSubject;
            this.TextBox_FilterTeacher.Text = DataAccess.TeacherDataAccess.getTeacherByCI(subjectToTeacher.ciTeacher).Name;
            this.Label_Teacher.Content = "Docente: " + DataAccess.TeacherDataAccess.getTeacherByCI(subjectToTeacher.ciTeacher).Name;
            this.Label_TeacherTeaching.Content = "Dicta la materia: " + this.selectedSubject.nameSubject;
            this.ComboBox_Gestion.SelectedItem = subjectToTeacher.gestion;
            this.ComboBox_Activity.SelectedValue = isInHorary.activity.ToString();
        }

        private void fillComboBox()
        {
            string Year = DateTime.Now.Year.ToString();
            for (int i = 1; i <= 12; i++)
            {
                this.ComboBox_Gestion.Items.Add(i + "-" + Year);
            }
            this.ComboBox_Gestion.SelectedIndex = 0;
        }

        public Model.Subject selectedSubject
        {
            //get { return (Model.Subject)this.DataGrid_SubjectResult.SelectedItem; }
            get { return (Model.Subject)this.subjectSelected; }
            set { this.subjectSelected = value; }
        }
        public Teacher selectedTeacher
        {
            get { return (Teacher)this.DataGrid_TeacherResult.SelectedItem; }
        }
        public Horary isInHorary
        {
            set { this.isInHoraryObject = value; }
            get { return (this.isInHoraryObject); }
        }

        public string filterSubject
        {
            //get { return this.TextBox_FilterSubject.Text; }
            get { return ""; }
        }
        public Collection<Model.Subject> listResultSubject
        {
            //set { this.DataGrid_SubjectResult.ItemsSource = value; }
            set { Collection<Model.Subject> l = value; }
        }

        public string filterTeacher
        {
            get { return (this.TextBox_FilterTeacher.Text); }
        }
        public Collection<Teacher> listResultTeacher
        {
            set { this.DataGrid_TeacherResult.ItemsSource = value; }
        }

        public bool isSubjectSearchByName
        {
            //get { return this.RadioButton_subjectByName.IsChecked == true; }
            get { return true; }
        }
        public bool isSubjectSearchByCode
        {
            //get { return this.RadioButton_subjectByCode.IsChecked == true; }
            get { return true; }
        }
        public bool isSubjectSearchByAcronym
        {
            //get { return this.RadioButton_subjectByAcronym.IsChecked == true; }
            get { return true; }
        }

        public bool isTeacherSearchByName
        {
            get { return this.RadioButton_teacherByName.IsChecked == true; }
        }
        public bool isTeacherSearchByCode
        {
            get { return this.RadioButton_teacherByCode.IsChecked == true; }
        }
        public bool isTeacherSearchByAcronym
        {
            get { return this.RadioButton_teacherByAcronym.IsChecked == true; }
        }

        public string message
        {
            set { this.Message = value; }
        }
        public string teacherLabel
        {
            set { this.Label_Teacher.Content = value; }
        }
        public string teacherTeachingLabel
        {
            set { this.Label_TeacherTeaching.Content = value; }
        }

        private void TextBox_TextChangedTeacherFilter(object sender, TextChangedEventArgs e)
        {

        }

        private void TextBox_TextChangedSubjectFilter(object sender, TextChangedEventArgs e)
        {
            presenter.SearchSubjectInRealTime();
        }

        private void Button_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_AsignHorary(object sender, RoutedEventArgs e)
        {
            HorarySolution hs = new HorarySolution(this.isInHorary);
            hs.ShowDialog();
        }

        private void Button_Click_Save(object sender, RoutedEventArgs e)
        {
            try
            {
                if (selectedSubject.code >0 && selectedTeacher.CI > 0)
                {
                    SubjectToTeacher stt = DataAccess.SubjectToTeacherDataAccess.newSubjectToTeacher(selectedTeacher.CI, selectedSubject.code, ComboBox_Gestion.SelectedItem.ToString());             
                    DataAccess.HoraryDataAccess.newHorary(isInHoraryObject.listHoraryDays, stt.code, ComboBox_Gestion.SelectedItem.ToString(), this.ComboBox_Activity.SelectedValue.ToString());
                    MessageBox.Show("Se guardo correctamente los datos", "Mensaje");
                    this.Close();
                }
                else
                    MessageBox.Show("Seleccione al menos un docente y una materia");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Seleccione al menos un docente y una materia", "Mensaje");
            }
        }

        private void SelectTeacher(object sender, MouseButtonEventArgs e)
        {
            presenter.SelectedTeacherOnCollection();
        }

        private void SelectSubject(object sender, MouseButtonEventArgs e)
        {
            presenter.SelectedSubjectOnCollection();
        }

        private void FocusSelectSubject(object sender, RoutedEventArgs e)
        {
            //if (this.DataGrid_SubjectResult.CurrentCell != null)
            //{
            //    Model.Subject aux = (Model.Subject)this.DataGrid_SubjectResult.CurrentCell.Item;
            //    this.DataGrid_SubjectResult.SelectedItem = aux;
            //    presenter.SelectedSubjectOnCollection();
            //}
        }

        private void FocusSelectTeacher(object sender, RoutedEventArgs e)
        {
            if (this.DataGrid_TeacherResult.CurrentCell != null)
            {
                Model.Teacher aux = (Model.Teacher)this.DataGrid_TeacherResult.CurrentCell.Item;
                this.DataGrid_TeacherResult.SelectedItem = aux;
                presenter.SelectedTeacherOnCollection();
            }
        }

        private void Button_Click_Modify(object sender, RoutedEventArgs e)
        {
            try
            {
                //Model.Subject subject = (Model.Subject)this.DataGrid_SubjectResult.SelectedItem;
                Model.Subject subject = selectedSubject;
                Model.Teacher teacher = (Model.Teacher)this.DataGrid_TeacherResult.SelectedItem;

                SubjectToTeacher stt = DataAccess.SubjectToTeacherDataAccess.modifySubjectToTeacher(subjectToTeacher.code, teacher.CI, subject.code, ComboBox_Gestion.SelectedItem.ToString());
                if (existHorary == false)
                {
                    DataAccess.HoraryDataAccess.newHorary(isInHoraryObject.listHoraryDays, subjectToTeacher.code, ComboBox_Gestion.SelectedItem.ToString(), this.ComboBox_Activity.SelectedValue.ToString());
                    MessageBox.Show("No existia un horario, se creo uno nuevo", "Mensaje");
                }
                else
                {
                    DataAccess.HoraryDataAccess.modifyHorary(isInHoraryObject.listHoraryDays, subjectToTeacher.code, ComboBox_Gestion.SelectedItem.ToString(), this.ComboBox_Activity.SelectedValue.ToString());
                    MessageBox.Show("Se Modifico correctamente los datos", "Mensaje");
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Mensaje");
            }
        }

        private void Button_SearchSubject_Click(object sender, RoutedEventArgs e)
        {            
            SearchSubject ss = new SearchSubject(this.subjectSelected);
            ss.ShowDialog();
            this.subjectSelected = ss.subject;
            if (this.subjectSelected != null)
                this.Label_TeacherTeaching.Content = "Dica la materia: " + this.subjectSelected.nameSubject;
            else
                MessageBox.Show("Seleccione una materia");
        }
    }
}
